<%@ page import="java.util.*,
                 com.actimind.actitime.util.*,
                 com.actimind.actitime.beans.*,
                 com.actimind.actitime.*,
                 org.apache.struts.util.ResponseUtils"%>
<%@ page errorPage="/error.jsp" %>
<%@ taglib uri="/WEB-INF/at.tld" prefix="at"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

<bean:define id="form" scope="request" name="NNCommentForm" type="com.actimind.actitime.user.NNCommentForm"/>

<html>
<head>
    <title><bean:write name="title-prefix" scope="application"/> <logic:equal value="true" name="form" property="editable">Edit </logic:equal>Time-Track Comment</title>
    <link rel="stylesheet" href="../css/at.css" type="text/css">

<script language="javascript">

var closing = false;
var laststate = "";
var ctrack = cday = 0;
var oImages;

// Function trims specified value
function trim(str)
{
    if(!str) return str;
    return str.replace(/^\s+/, "").replace(/\s+$/, "");
}

function setImage(imgSrc, ctrack, cday, firstRun) {
      f = window.opener.document.images["timeTrack[" + ctrack + "].commentImg[" + cday + "]"];
      if(f) {
             laststate = f.src;
             if(document.layers || !firstRun)
                f.src = imgSrc;
        }
}

function restoreImage() {
   if(!document.savecom.ctrack) return;
   if(!closing)
        if(oImages["timeTrack[" + ctrack + "].commentImg[" + cday + "]"]) {
             oImages["timeTrack[" + ctrack + "].commentImg[" + cday + "]"].src = laststate;
        }
}

 function SaveComment() {
    var ta = trim(document.savecom.comment.value);
    if(ta.length <= 2000) {
      // find a hidden element.
      var el = window.opener.document.SubmitTTForm.elements["timeTrack[" + ctrack + "].comment[" + cday + "]"];
      if(el)  {
                el.value = ta;
                if(trim(el.value) != trim(el.defaultValue)) {
                    if(!window.opener.dataModified[el.name])
                                window.opener.dataModified[el.name] = el;
                } else {
                      if(window.opener.dataModified[el.name])
                            delete dataModified[el.name];
                }
      }
      if(ta.length == 0) setImage("../img/comment_blank.gif", ctrack, cday, false)
         else setImage("../img/comment_entered.gif", ctrack, cday);
      closing = true;
      selectSpentStr();
      window.close();
    } else
       if(ta.length > 2000) {
           alert("Comments are too long.\n\nMaximum comments length is 2000 characters.\nCurrent comments length is " + ta.length + " characters.\nPlease shorten the comments.");
       }
 }


 function CloseWindow()
 {
      // find a hidden element.
      var f = window.opener.document.SubmitTTForm.elements["timeTrack[" + ctrack + "].comment[" + cday + "]"];
      var old_value = "";
      if(f) {
           old_value = trim(f.value);
      }
      var new_value = trim(document.savecom.comment.value);
      if(new_value != old_value)
        if(!confirm("Modifications made in the comments will be lost. Are you sure you want to continue?"))
          return;
    closing = true;
    setImage(laststate, ctrack, cday, true);
    selectSpentStr();
    window.close();
 }

    function selectSpentStr() {
       f = window.opener.document.SubmitTTForm.elements["timeTrack[" + ctrack + "].spentStr[" + cday + "]"];
       if(f) {
           f.focus();
       }
    }

    function prepareForm() {
        if(!document.savecom.ctrack) return;
        ctrack = document.savecom.ctrack.value;
        cday = document.savecom.cday.value;
        oImages = window.opener.document.images;
        // find a hidden element.
        var f = window.opener.document.SubmitTTForm.elements["timeTrack[" + ctrack + "].comment[" + cday + "]"];
        if(f)  {
                document.savecom.comment.value = f.value;
        }
        setImage("../img/comment_being_edited.gif", ctrack, cday, true);
    }


</script>

</head>
<body bgcolor="#6995de" onload="prepareForm();" onunload="restoreImage();">

<form name="savecom">
    <table width="100%" cellspacing="0" cellpadding="2" border="0">
    <tr>
        <td>
            <span class="whitetitle_nn"><logic:equal value="true" property="editable" name="form">Edit </logic:equal>Time-Track Comments</span>
        </td>
    </tr>
    </table>
    <table cellspacing="2" <logic:equal value="false" name="form" property="editable">cellpadding="8"</logic:equal>
         <logic:equal value="true" name="form" property="editable">cellpadding="2"</logic:equal> border="0">
    <tr align="left">
        <td valign="top" align="left" nowrap="1"><span class="white"><b>Customer/Project:</b></span></td>
        <td valign="top" align="left"><span class="white"><bean:write name="form" property="customer"/>/<bean:write name="form" property="project"/></span></td>
    </tr>
    <tr align="left">
        <td valign="top"><span class="white"><b>Task:</b></span></td>
        <td valign="top" align="left"><span class="white"><bean:write name="form" property="task"/></span></td>
    </tr>
    <tr align="left">
        <td valign="top"><span class="white"><b>Date:</b></span></td>
        <td nowrap valign="top" align="left"><span class="white"><bean:write name="form" property="dateFormatted"/></span></td>
    </tr>
    <logic:equal value="false" name="form" property="editable">
    <tr align="left"><td valign="top"><span class="white"><b>Comments:</b></span></td>
        <td valign="top"><span class="white"><bean:write property="comment" name="form"/></span></td>
    </tr>
    </logic:equal>
    <logic:equal value="true" name="form" property="editable"></table><table cellspacing="2" cellpadding="2"border="0">
    <tr align="left"><td colspan="2" valign="top"><span class="white"><b>Comments:</b></span></td>
    </tr>
    <tr align="left">
        <td colspan="2">
            <textarea name="comment" rows="5" cols="45" wrap="virtual"><bean:write name="form" property="comment" /></textarea>
            <input type="hidden" name="ctrack" value="<bean:write name="form" property="taskId" />">
            <input type="hidden" name="cday" value="<bean:write name="form" property="dayOfWeek" />">
        </td>
    </tr>
    </logic:equal>
    </table>

    <logic:equal value="true" property="editable" name="form">
    <br>
    <table width="100%" cellspacing="0" cellpadding="0" border="0">
    <tr>
        <td align="right">
            <input type="button" width="24" class="infobutton" value="   OK   " name="nscbutton" onclick="SaveComment();">
        </td>
        <td><img src="../img/pixel.gif" width="60" height="1" alt=""></td>
        <td align="left">
            <input type="button" value="Cancel " class="infobutton" name="nccbutton" onclick="CloseWindow();">
        </td>
    </tr>
    </table>
    </logic:equal>

    <logic:equal value="false" property="editable" name="form">
    <br>
    <table width="100%" cellspacing="0" cellpadding="0" border="0">
    <tr>
        <td align="center">
            <a href="javascript:window.close()"><span class="white">Close Window</span></a>
        </td>
    </tr>
    </table>
    </logic:equal>
</form>
</body>
</html>
